package com.leoman.plz_p.presenter;


import com.leoman.plz_p.interfaces.StudyView;
import com.leoman.plz_p.module.api.CourseApi;
import com.leoman.plz_p.module.api.ExamApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.IdsBean;
import com.leoman.plz_p.module.bean.MapData;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.module.bean.RecentExamBean;
import com.leoman.plz_p.module.bean.TestExamBean;
import com.leoman.plz_p.subscriber.ResponseSubscriber;
import com.leoman.plz_p.utils.UserHelper;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class StudyPresenter extends BasePresenter<StudyView> {

    private CourseApi courseApi;
    private ExamApi examApi;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        courseApi=getApi(CourseApi.class);
        examApi=getApi(ExamApi.class);
    }


    public void getExamList(String name){
        getView().showLoading();
        courseApi.getExamList(UserHelper.getToken(),name,1+"",Integer.MAX_VALUE+"")
                .compose(new ResponseTransformer<>(this.<BaseData<PageListData<TestExamBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<TestExamBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<TestExamBean>> pageListDataBaseData) {
                        if (checkListNotNull(pageListDataBaseData.data.getRows()))
                        view.obtainExamList(pageListDataBaseData.data.getRows());
                    }
                });
    }

    public void getRecentExamInfo(){
        courseApi.getRecentExamInfo(UserHelper.getToken())
                .compose(new ResponseTransformer<>(this.<BaseData<MapData<RecentExamBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData<RecentExamBean>>>(view) {
                    @Override
                    public void success(BaseData<MapData<RecentExamBean>> mapDataBaseData) {
                        if (checkDataNotNull(mapDataBaseData))
                        view.obtainRecentExamInfo(mapDataBaseData.data);
                    }
                });
    }

    public void getHasDoExamInfo(String examId){
        examApi.getHasDoExamInfo(UserHelper.getToken(),examId).compose(new ResponseTransformer<>(this.<BaseData<IdsBean>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<IdsBean>>(view) {
                    @Override
                    public void success(BaseData<IdsBean> data) {
                        view.obtainHasDoExamInfo(data.data);
                    }
                });
    }

    public void getExamIds(String examId){
        examApi.getExamIds(UserHelper.getToken(),examId).compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> mapDataBaseData) {
                        view.obtainExamInfo(mapDataBaseData.data);
                    }
                });
    }


    public void getExamIds2(String examId){
        examApi.getExamIds(UserHelper.getToken(),examId).compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> mapDataBaseData) {
                        view.obtainIds(mapDataBaseData.data.ids);
                    }
                });
    }


    @Override
    public void onStart() {
        getExamList(null);
        getRecentExamInfo();
    }
}